package com.pro_one.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pro_one.constant.QiniuConstant;
import com.pro_one.constant.RedisConstant;
import com.pro_one.dao.OrderAddressDao;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.exception.CheckItemDeleteFailException;
import com.pro_one.exception.OrderAddressDeleteFailException;
import com.pro_one.pojo.OrderAddress;
import com.pro_one.pojo.Setmeal;
import com.pro_one.service.OrderAddressService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service(interfaceClass = OrderAddressService.class)
@Transactional
public class OrderAddressServiceImpl implements OrderAddressService {
    @Autowired
    private OrderAddressDao orderAddressDao;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 新增预约地址信息
     *
     * @param orderAddress
     */
    @Override
    public void add(OrderAddress orderAddress) {
        String img = orderAddress.getImg();
        String imgName = "";
        if (img != null) {
            imgName = img.substring(QiniuConstant.UPLOAD_URL.length());
            orderAddress.setImg(imgName);
        }
        orderAddressDao.add(orderAddress);
        if (img != null) {
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, imgName);
        }
    }

    /**
     * 列表查询展示
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findByCondition(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        List<OrderAddress> list = orderAddressDao.findByCondition(queryString);
        PageInfo pageInfo = new PageInfo(list);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 查询单个预约地址信息
     *
     * @param id
     * @return
     */
    @Override
    public OrderAddress findById(Integer id) {
        OrderAddress orderAddress = orderAddressDao.findById(id);
        orderAddress.setImg(QiniuConstant.UPLOAD_URL + orderAddress.getImg());
        return orderAddress;
    }

    /**
     * 修改预约地址信息
     *
     * @param orderAddress
     */
    @Override
    public void edit(OrderAddress orderAddress) {
        Integer id = orderAddress.getId();
        String oldImg = orderAddressDao.findById(id).getImg();
        String newImg = orderAddress.getImg();
        if (oldImg != null) {
            if (!oldImg.equals(newImg)) {
                String finalImgName = newImg.substring(QiniuConstant.UPLOAD_URL.length());
                orderAddress.setImg(finalImgName);
                jedisPool.getResource().smove(RedisConstant.SETMEAL_PIC_DB_RESOURCES, RedisConstant.SETMEAL_PIC_RESOURCES, oldImg);
                jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, finalImgName);
            }
        }
        orderAddressDao.edit(orderAddress);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<OrderAddress> findAll() {
        return orderAddressDao.findAll();
    }

    /**
     * 根据地址查询地址详情信息
     *
     * @param address
     * @return
     */
    @Override
    public OrderAddress findOrderAddressDetailByAddress(String address) {
        return orderAddressDao.findOrderAddressDetailByAddress(address);
    }

    /**
     * 删除地址，如果存在预约信息，则删除失败
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        int count = orderAddressDao.findOrderCountById(id);
        if (count > 0) {
            throw new OrderAddressDeleteFailException();
        }
        orderAddressDao.deleteById(id);
    }
}
